<?php

require_once 'PHPUnit/Framework.php';

CCore::Using('mime.message');

/**
 * Test class for CCoreMimeMessage.
 */
class CCoreMimeMessageTest extends PHPUnit_Framework_TestCase
{

	/**
	 * @var CCoreMimeMessage
	 */
	protected $object;

	/**
	 * This method is called before a test is executed.
	 */
	protected function setUp()
	{
		$this->object = new CCoreMimeMessage();
	}

	public function testSubject()
	{
		$this->object->ParseFromFile('../examples/files.testMails/uid-01.eml');

		$this->assertEquals('TEST HTML MESSAGE WITH ATTACHMENT', $this->object->Subject());
	}

	public function testDate()
	{
		$this->object->ParseFromFile('../examples/files.testMails/uid-01.eml');

		$this->assertEquals('Thu, 6 May 2010 14:26:37 +0000', $this->object->DateObjToString(
			$this->object->DateAsObject(), 'r'));
		$this->assertEquals('Thu, 6 May 2010 15:26:37 +0100', $this->object->DateObjToString(
			$this->object->DateAsObject(), 'r', 3600));
		$this->assertEquals('05-26-37, 6-05-10', $this->object->DateObjToString(
			$this->object->DateAsObject(), 'h-i-s, j-m-y', 3600 * 3));

		$this->object->Headers->Clear();
		$this->assertEquals('', $this->object->DateObjToString(
			$this->object->DateAsObject(), 'r'));

		$this->assertEquals('', $this->object->DateObjToString(
			$this->object->ReceivedDateAsObject(), 'r'));

		$oMimeHeader = new CCoreMimeHeader(C_MIME_HEADERS_RECEIVED, 'Thu, 6 May 2010 14:26:37 +0000');
		$this->object->Headers->Add($oMimeHeader);

		$this->assertEquals('Thu, 6 May 2010 14:26:37 +0000', $this->object->DateObjToString(
			$this->object->ReceivedDateAsObject(), 'r'));
		$this->assertEquals('03-26-37, 6-05-10', $this->object->DateObjToString(
			$this->object->ReceivedDateAsObject(), 'h-i-s, j-m-y', 3600));

	}

	public function testMail()
	{
		$this->object->ParseFromFile('../examples/files.testMails/uid-01.eml');

		$oFrom = $this->object->From();
		$this->assertType('CCoreMimeEmail', $oFrom);
		$this->assertEquals('from@domain.com', $oFrom->Email());
		$this->assertEquals('', $oFrom->DisplayName());
		$this->assertEquals('', $oFrom->Remarks());

		$oTo = $this->object->To();
		$this->assertType('CCoreMimeEmailCollection', $oTo);
		$this->assertEquals(1, $oTo->Count());
		$oFirst = $oTo->GetByIndex(0);
		$this->assertType('CCoreMimeEmail', $oFirst);
		$this->assertEquals('test@domain.com', $oFirst->Email());
		$this->assertEquals('', $oFirst->DisplayName());
		$this->assertEquals('', $oFirst->Remarks());
	
		$oCc = $this->object->Cc();
		$this->assertNull($oCc);
		
		$oBcc = $this->object->Bcc();
		$this->assertNull($oBcc);
	}

	public function testParseFromStream0()
	{
		$this->object->ParseFromFile('../examples/files.testMails/uid-02.eml');

		$oHeader =& $this->object->Headers->GetByIndex(7);
		$this->assertEquals('<6800462619664130307453@TEST-TEST>', $oHeader->RawValue());

		$this->assertEquals(251, strlen($this->object->Headers->GetRawHeaders()));

		$oBodyText =& $this->object->BodyText->GetByIndex(0);
		$this->assertFalse($oBodyText->IsHtml());

		$this->assertEquals(0, $this->object->Attachments->Count());
	}

	public function testParseFromStream1()
	{
		$this->object->ParseFromFile('../examples/files.testMails/uid-04.eml');

		$oBodyText =& $this->object->BodyText->GetByIndex(0);
		$this->assertTrue($oBodyText->IsHtml());

		file_put_contents('c:/a.txt', print_r($this->object->Attachments, true));
		$this->assertEquals(3, $this->object->Attachments->Count());

		$oAttach =& $this->object->Attachments->GetByIndex(0);
		$this->assertEquals('av-7.gif', $oAttach->Name());
		$this->assertEquals(327, $oAttach->EstimatedSize());

		$oAttach2 =& $this->object->Attachments->GetByIndex(1);
		$this->assertEquals('image.gif', $oAttach2->Name());
		$this->assertEquals(327, $oAttach2->EstimatedSize());

		$oAttach3 =& $this->object->Attachments->GetByIndex(2);
		$this->assertEquals('111222.gif', $oAttach3->Name());
		$this->assertEquals(28, $oAttach3->EstimatedSize());
	}

	public function testParseFromStream2()
	{
		$this->object->ParseFromString('MIME-Version: 1.0
From: from@domain.com
To: test@domain.com

555');
		$this->assertEquals(3, $this->object->Headers->Count());

		$rBinaryBodyStream = $this->object->GetBinaryBodyStream();
		$this->assertEquals('555', stream_get_contents($rBinaryBodyStream));

		$this->object->Headers = new CCoreMimeHeadersCollection('MIME-Version: 1.0
To: test@domain.com');
		$this->assertEquals(2, $this->object->Headers->Count());
	}

	public function testParseFromStream3()
	{
		$this->object->ParseFromFile('../examples/files.testMails/uid-01.eml');

		$oBodyText =& $this->object->BodyText->GetByIndex(0);
		$rH = $oBodyText->GetBodyStream();
		$rH = $oBodyText->GetBinaryBodyStream();
		$this->assertEquals('<html><body>TEST HTML MESSAGE WITH ATTACHMENT</body></html>',
			trim(stream_get_contents($rH)));

		$oAttachments =& $this->object->Attachments->GetByIndex(0);
		$rH = $oAttachments->GetBodyStream();
		$rH = $oAttachments->GetBinaryBodyStream();

		$this->assertEquals(11600, strlen(stream_get_contents($rH)));
	}
}
